package com.jyw.web.controller.system;

import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.page.TableDataInfo;
import com.jyw.system.domain.SysMessage;
import com.jyw.system.domain.dto.SysMessageEditDto;
import com.jyw.system.domain.dto.SysMessagePageDto;
import com.jyw.system.domain.vo.SysMessageVo;
import com.jyw.system.service.ISysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 系统消息Controller
 *
 * @author zhangcheng
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/system/message")
@Api(tags = "系统消息")
public class SysMessageController extends BaseController {
    @Resource
    private ISysMessageService sysMessageService;

    /**
     * 分页查询用户系统消息
     */
    @GetMapping("/list")
    @ApiOperation("分页查询用户系统消息")
    public TableDataInfo<SysMessageVo> list(SysMessagePageDto pageDto) {
        startPage();
        pageDto.setReceiver(getUserId());
        List<SysMessageVo> list = sysMessageService.pageList(pageDto);
        return getDataTable(list);
    }

    /**
     * 查询用户系统未读消息
     *
     * @return
     */
    @GetMapping("/getCount")
    @ApiOperation("查询用户系统未读消息")
    public CommonResult<Long> getCount(){
        Long userId=getUserId();
        Long count= sysMessageService.getCount(userId);
        return CommonResult.success("成功",count);
    }

    /**
     * 获取系统消息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取系统消息详细信息")
    public CommonResult<SysMessageVo> getInfo(@PathVariable("id") Long id) {
        SysMessageVo sysMessageVo=sysMessageService.selectSysMessageById(id);
        return CommonResult.success(sysMessageVo);
    }


    /**
     * 修改系统消息
     */
    @PutMapping(value = "edit")
    @ApiOperation("修改系统消息")
    public CommonResult edit(@RequestBody @Valid SysMessageEditDto editDto) {
        sysMessageService.edit(editDto);
        return CommonResult.success();
    }

    /**
     * 批量标记已读
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "batchReading/{ids}")
    @ApiOperation("批量标记已读")
    public CommonResult batchReading(@PathVariable Long[] ids){
        sysMessageService.batchReading(ids);
        return CommonResult.success();
    }

    /**
     * 全部标记已读
     *
     * @return
     */
    @PostMapping(value = "allReading")
    @ApiOperation("全部标记已读")
    public CommonResult allReading(){
        Long userId=getUserId();
        sysMessageService.allReading(userId);
        return CommonResult.success();
    }
}
